package com.example.demo.common.utils.demo;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.utils.BaseResponse;
import com.example.demo.common.utils.HttpUtil;
import com.example.demo.common.utils.RSAHelper;
import com.example.demo.common.utils.demo.common.Config;
import com.example.demo.common.utils.demo.model.OpenApiCreateInvoiceRequest;
import com.example.demo.common.utils.demo.model.OpenApiUnInvoiceRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Created by zhangws on 2019/11/4 15:16.
 * 发票
 */
public class Invoices {
    static Logger log = LoggerFactory.getLogger(Invoices.class);

    /**
     * 获取可开发票订单列表
     */
    public static void queryUnbilled(OpenApiUnInvoiceRequest openApiUnInvoiceRequest) {
        String url = Config.baseUrl + "/open/api/v1/invoices/queryUnbilled";
        String paramsJson = JSONObject.toJSONString(openApiUnInvoiceRequest);
        BaseResponse baseResponse = HttpUtil.post(url, paramsJson, Config.appKey, Config.secretkey, Config.publicKey);
        String rs = baseResponse.getData() != null ? baseResponse.getData().toString() : "";
        //公钥解密
        if (StringUtils.isNotBlank(rs)) {
            try {
                String dec = RSAHelper.decryptByPublicKey(rs, Config.publicKey);
                log.info("解密结果:{}", dec);
            } catch (Exception e) {
                log.error("publicKey 有误");
            }
        }
    }

    /**
     * 开票
     * thirdOrderIds
     */
    public static void createInvoice(OpenApiCreateInvoiceRequest openApiUnInvoiceRequest) {
        String url = Config.baseUrl + "/open/api/v1/invoices/create";
        String paramsJson = JSONObject.toJSONString(openApiUnInvoiceRequest);
        BaseResponse baseResponse = HttpUtil.post(url, paramsJson, Config.appKey, Config.secretkey, Config.publicKey);
        String rs = baseResponse.getData() != null ? baseResponse.getData().toString() : "";
        //公钥解密
        if (StringUtils.isNotBlank(rs)) {
            try {
                String dec = RSAHelper.decryptByPublicKey(rs, Config.publicKey);
                log.info("解密结果:{}", dec);
            } catch (Exception e) {
                log.error("publicKey 有误");
            }
        }
    }

    /**
     * 开票结果查询
     * thirdOrderIds
     */
    public static void detail(String invoiceId) {
        String url = Config.baseUrl + "/open/api/v1/invoices/detail";
        String paramsJson = "{\"invoiceId\":\"" + invoiceId + "\"}";
        BaseResponse baseResponse = HttpUtil.post(url, paramsJson, Config.appKey, Config.secretkey, Config.publicKey);
        String rs = baseResponse.getData() != null ? baseResponse.getData().toString() : "";
        //公钥解密
        if (StringUtils.isNotBlank(rs)) {
            try {
                String dec = RSAHelper.decryptByPublicKey(rs, Config.publicKey);
                log.info("解密结果:{}", dec);
            } catch (Exception e) {
                log.error("publicKey 有误");
            }
        }
    }

    /**
     * 上传本地文件
     */
    public static void uploadFile(String localPath) {
        String url = Config.baseUrl + "/open/api/v1/invoices/uploadFile";
        String paramsJson = "{\"locaPath\":\"" + localPath + "\"}";
        BaseResponse baseResponse = HttpUtil.post(url, paramsJson, Config.appKey, Config.secretkey, Config.publicKey);
        String rs = baseResponse.getData() != null ? baseResponse.getData().toString() : "";
        //公钥解密
        if (StringUtils.isNotBlank(rs)) {
            try {
                String dec = RSAHelper.decryptByPublicKey(rs, Config.publicKey);
                log.info("解密结果:{}", dec);
            } catch (Exception e) {
                log.error("publicKey 有误");
            }
        }
    }

}
